package com.ktgj.flight.service.impl;

import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.common.map.GaoDeUtil;
import com.ktgj.flight.domain.KtserviceAirport;
import com.ktgj.flight.mapper.KtserviceAirportMapper;
import com.ktgj.flight.service.IKtserviceAirportService;
import com.ktgj.flight.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
 * 机场、高铁、港口Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-10
 */
@Service
public class KtserviceAirportServiceImpl implements IKtserviceAirportService 
{
    @Autowired
    private KtserviceAirportMapper ktserviceAirportMapper;

    //@Autowired
    //private RemoteService remoteService;

    /**
     * 查询机场、高铁、港口
     * 
     * @param airportCode 机场、高铁、港口ID
     * @return 机场、高铁、港口
     */
    @Override
    public KtserviceAirport selectKtserviceAirportById(String airportCode)
    {
        return ktserviceAirportMapper.selectKtserviceAirportById(airportCode);
    }

    /**
     * 查询机场、高铁、港口列表
     * 
     * @param ktserviceAirport 机场、高铁、港口
     * @return 机场、高铁、港口
     */
    @Override
    public List<KtserviceAirport> selectKtserviceAirportList(KtserviceAirport ktserviceAirport)
    {
        return ktserviceAirportMapper.selectKtserviceAirportList(ktserviceAirport);
    }

    /**
     * 新增机场、高铁、港口
     * 
     * @param ktserviceAirport 机场、高铁、港口
     * @return 结果
     */
    @Override
    public int insertKtserviceAirport(KtserviceAirport ktserviceAirport)
    {
        ktserviceAirport.setCreateTime(DateUtils.getNowDate());
        return ktserviceAirportMapper.insertKtserviceAirport(ktserviceAirport);
    }

    /**
     * 修改机场、高铁、港口
     * 
     * @param ktserviceAirport 机场、高铁、港口
     * @return 结果
     */
    @Override
    public int updateKtserviceAirport(KtserviceAirport ktserviceAirport)
    {
        ktserviceAirport.setUpdateTime(DateUtils.getNowDate());
        return ktserviceAirportMapper.updateKtserviceAirport(ktserviceAirport);
    }

    /**
     * 批量删除机场、高铁、港口
     * 
     * @param airportCodes 需要删除的机场、高铁、港口ID
     * @return 结果
     */
    @Override
    public int deleteKtserviceAirportByIds(String[] airportCodes)
    {
        return ktserviceAirportMapper.deleteKtserviceAirportByIds(airportCodes);
    }

    /**
     * 删除机场、高铁、港口信息
     * 
     * @param airportCode 机场、高铁、港口ID
     * @return 结果
     */
    @Override
    public int deleteKtserviceAirportById(String airportCode)
    {
        return ktserviceAirportMapper.deleteKtserviceAirportById(airportCode);
    }

    /**
     * 根据经纬度查询城市名称
     * */
    public String cityName(String lng, String lat){
        //拼接经纬度
        String lngAndLat=lng+","+lat;

        //调用方法查询城市名称
        GaoDeUtil deUtil = new GaoDeUtil();
        String cityName=deUtil.getCityByCoordinate(lngAndLat);
        return cityName;
    }

    /**
     * 计算两坐标距离
     * */
    public Double distanceMeter(String lng, String lat, String strLngAndLat){
        //拼接经纬度
        String lngAndLat=lng+","+lat;

        //调用方法计算距离
        GaoDeUtil deUtil = new GaoDeUtil();
        Double distance=deUtil.getDistanceMeter(lngAndLat, strLngAndLat);
        return distance;
    }

    /**
     * 根据当前位置查询最近网点
     * */
    @Override
    public KtserviceAirportVo selectKtserviceAirportByLngAndLat(String lng, String lat) {
        //获取城市名称
        String cityName = cityName(lng, lat);

        //调用方法查询城市对应ID
        List<KtserviceAirport> cityIdList=ktserviceAirportMapper.selectKtserviceAirportByCityName(cityName);

        Long cityId = 0l;

        for (KtserviceAirport ka : cityIdList) {
            cityId=ka.getCityId();
        }

        //调用方法查询所有对应网点
        List<KtserviceAirport> ktserviceAirportList=ktserviceAirportMapper.selectKtserviceAirportByCityId(cityId);
        List<KtserviceAirportVo> ktserviceAirportVoList = new ArrayList<>();

        //分类标识
        int Logo=1;

        for (int i=0;i<=2;i++){
            //遍历集合，将需要的数据填充到ktserviceAirportVoList集合中
            for (KtserviceAirport ktserviceAirport:ktserviceAirportList){
                //根据分类筛选，筛选顺序为：机场-》高铁-》港口
                if (ktserviceAirport.getClassIficationType()==Logo){
                    KtserviceAirportVo ktserviceAirportVo = new KtserviceAirportVo();
                    ktserviceAirportVo.setAirportCode(ktserviceAirport.getAirportCode());
                    ktserviceAirportVo.setAirportName(ktserviceAirport.getAirportName());
                    ktserviceAirportVo.setCityId(ktserviceAirport.getCityId());
                    ktserviceAirportVo.setCityName(ktserviceAirport.getCityName());
                    ktserviceAirportVo.setLat(ktserviceAirport.getLat());
                    ktserviceAirportVo.setLng(ktserviceAirport.getLng());
                    ktserviceAirportVo.setClassIficationType(ktserviceAirport.getClassIficationType());
                    ktserviceAirportVoList.add(ktserviceAirportVo);
                }
            }

            //ktserviceAirportVoList集合为空则进入下一级重新筛选
            if (ktserviceAirportVoList.size()==0){
                Logo=Logo+1;
            }
            else {//ktserviceAirportVoList集合不为空，则跳出循环，拿到数据
                break;
            }
        }

        //将距离赋给对应的对象
        Double distance = 0.0;
        List<KtserviceAirportVo> ktserviceAirportVos=new ArrayList<>();
        for (KtserviceAirportVo ktserviceAirportVo:ktserviceAirportVoList){
            String lngNum = ktserviceAirportVo.getLng();
            int iLng = lngNum.indexOf(".");
            String latNum = ktserviceAirportVo.getLat();
            int iLat = latNum.indexOf(".");

            if (lngNum.substring(iLng+1, ktserviceAirportVo.getLng().length()).length()>6){
                //经度保留小数点后六位
                ktserviceAirportVo.setLng(lngNum.substring(0,iLng+1)+lngNum.substring(iLng+1,iLng+7));
            }
            if (latNum.substring(iLat+1, ktserviceAirportVo.getLat().length()).length()>6){
                //纬度保留小数点后六位
                ktserviceAirportVo.setLat(latNum.substring(0,iLat+1)+latNum.substring(iLat+1,iLat+7));
            }

            String strLngAndLat=ktserviceAirportVo.getLng()+","+ktserviceAirportVo.getLat();
            distance=distanceMeter(lng, lat, strLngAndLat);
            ktserviceAirportVo.setDistance(distance);
            ktserviceAirportVos.add(ktserviceAirportVo);
        }

        /**
         * 按照当前坐标与机场的距离进行升序排列
         * */
        ktserviceAirportVos.sort((x, y) -> Double.compare(x.getDistance(), y.getDistance()));

        if (ktserviceAirportVos.size()>0){
            return ktserviceAirportVos.get(0);
        }
        return null;
    }

    /**
     * 查询机场，高铁，港口列表(显示)
     * */
    @Override
    public List<KtserviceAirportSelectVo> selectKtserviceAirportSelectVo(Long classIficationType, String lng, String lat, int count) {
        List<KtserviceAirport> ktserviceAirportList=ktserviceAirportMapper.selectKtserviceAirportSelectVo(classIficationType);
        List<KtserviceAirportSelectVo> ktserviceAirportSelectVoList = new ArrayList<>();
        //获取城市名称
        String city = cityName(lng, lat);

        if (count == 1){//查询有网点的机场，高铁，港口
            //遍历集合，将需要的数据去除，填充到ktserviceAirportSelectVoList中
            for (KtserviceAirport ktserviceAirport : ktserviceAirportList) {
                boolean flagViphall = false;//remoteService.selectVipHallVoByAirportCode(ktserviceAirport.getAirportCode());
                boolean flagVipchannel =false; //remoteService.selectVipchannelByAirportCode(ktserviceAirport.getAirportCode());
                if (flagViphall || flagVipchannel){
                    KtserviceAirportSelectVo ktserviceAirportSelectVo=new KtserviceAirportSelectVo();
                    ktserviceAirportSelectVo.setAirportCode(ktserviceAirport.getAirportCode());
                    ktserviceAirportSelectVo.setAirportName(ktserviceAirport.getAirportName());
                    if (ktserviceAirport.getPopular().equals("popular")){
                        ktserviceAirportSelectVo.setPopular("true");
                    }
                    else {
                        ktserviceAirportSelectVo.setPopular("false");
                    }
                    String letter=ktserviceAirport.getShortPinyin().substring(0,1);
                    String uppercaseLetter = letter.toUpperCase();
                    ktserviceAirportSelectVo.setLetter(uppercaseLetter);
                    ktserviceAirportSelectVo.setCurrentCity(ktserviceAirport.getCityName());
                    List<KtserviceAirport> cityIdList=ktserviceAirportMapper.selectKtserviceAirportByCityName(city);

                    Long cityId = 0l;

                    for (KtserviceAirport ka : cityIdList) {
                        cityId=ka.getCityId();
                    }

                    if (cityId.equals(ktserviceAirport.getCityId())){
                        ktserviceAirportSelectVo.setWhetherCurrentCity("yes");
                    }
                    else {
                        ktserviceAirportSelectVo.setWhetherCurrentCity("no");
                    }

                    String lngAndLat = ktserviceAirport.getLng()+","+ktserviceAirport.getLat();

                    String lngNum = ktserviceAirport.getLng();
                    int iLng = lngNum.indexOf(".");
                    String latNum = ktserviceAirport.getLat();
                    int iLat = latNum.indexOf(".");

                    if (lngNum.substring(iLng+1, ktserviceAirport.getLng().length()).length()>6){
                        //经度保留小数点后六位
                        ktserviceAirport.setLng(lngNum.substring(0,iLng+1)+lngNum.substring(iLng+1,iLng+7));
                    }
                    if (latNum.substring(iLat+1, ktserviceAirport.getLat().length()).length()>6){
                        //纬度保留小数点后六位
                        ktserviceAirport.setLat(latNum.substring(0,iLat+1)+latNum.substring(iLat+1,iLat+7));
                    }

                    ktserviceAirportSelectVo.setDistance(distanceMeter(lng, lat, lngAndLat));

                    ktserviceAirportSelectVoList.add(ktserviceAirportSelectVo);
                }
            }
        }
        else if (count == 2){//查询全部机场，高铁，港口
            for (KtserviceAirport ktserviceAirport : ktserviceAirportList) {
                KtserviceAirportSelectVo ktserviceAirportSelectVo=new KtserviceAirportSelectVo();
                ktserviceAirportSelectVo.setAirportCode(ktserviceAirport.getAirportCode());
                ktserviceAirportSelectVo.setAirportName(ktserviceAirport.getAirportName());
                if (ktserviceAirport.getPopular().equals("popular")){
                    ktserviceAirportSelectVo.setPopular("true");
                }
                else {
                    ktserviceAirportSelectVo.setPopular("false");
                }
                String letter=ktserviceAirport.getShortPinyin().substring(0,1);
                String uppercaseLetter = letter.toUpperCase();
                ktserviceAirportSelectVo.setLetter(uppercaseLetter);
                ktserviceAirportSelectVo.setCurrentCity(ktserviceAirport.getCityName());
                List<KtserviceAirport> cityIdList=ktserviceAirportMapper.selectKtserviceAirportByCityName(city);

                Long cityId = 0l;

                for (KtserviceAirport ka : cityIdList) {
                    cityId=ka.getCityId();
                }

                if (cityId.equals(ktserviceAirport.getCityId())){
                    ktserviceAirportSelectVo.setWhetherCurrentCity("yes");
                }
                else {
                    ktserviceAirportSelectVo.setWhetherCurrentCity("no");
                }

                String lngAndLat = ktserviceAirport.getLng()+","+ktserviceAirport.getLat();

                String lngNum = ktserviceAirport.getLng();
                int iLng = lngNum.indexOf(".");
                String latNum = ktserviceAirport.getLat();
                int iLat = latNum.indexOf(".");

                if (lngNum.substring(iLng+1, ktserviceAirport.getLng().length()).length()>6){
                    //经度保留小数点后六位
                    ktserviceAirport.setLng(lngNum.substring(0,iLng+1)+lngNum.substring(iLng+1,iLng+7));
                }
                if (latNum.substring(iLat+1, ktserviceAirport.getLat().length()).length()>6){
                    //纬度保留小数点后六位
                    ktserviceAirport.setLat(latNum.substring(0,iLat+1)+latNum.substring(iLat+1,iLat+7));
                }

                ktserviceAirportSelectVo.setDistance(distanceMeter(lng, lat, lngAndLat));

                ktserviceAirportSelectVoList.add(ktserviceAirportSelectVo);
            }
        }

        return ktserviceAirportSelectVoList;
    }





    /**
     * 查询热门机场，高铁，港口列表(显示)
     * */
    @Override
    public List<KtserviceAirportSelectVo> selectKtserviceAirportByPopular(Long classIficationType) {
        List<KtserviceAirport> ktserviceAirportList=ktserviceAirportMapper.selectKtserviceAirportByPopular(classIficationType);
        List<KtserviceAirportSelectVo> ktserviceAirportSelectVoList = new ArrayList<>();
        //遍历集合，将需要的数据去除，填充到ktserviceAirportSelectVoList中
        for (KtserviceAirport ktserviceAirport : ktserviceAirportList) {
            KtserviceAirportSelectVo ktserviceAirportSelectVo=new KtserviceAirportSelectVo();
            ktserviceAirportSelectVo.setAirportCode(ktserviceAirport.getAirportCode());
            ktserviceAirportSelectVo.setAirportName(ktserviceAirport.getAirportName());
            ktserviceAirportSelectVoList.add(ktserviceAirportSelectVo);
        }
        return ktserviceAirportSelectVoList;
    }

    /**
     * 查询全部高铁
     * */
    @Override
    public List<TrainVo> selectKtserviceAirportByCities(String lng, String lat) {
        List<KtserviceAirport> ktserviceAirportList = ktserviceAirportMapper.selectKtserviceAirportByCities();
        List<TrainVo> trainVoList = new ArrayList<>();
        String cityName = cityName(lng, lat);
        for (KtserviceAirport ktserviceAirport : ktserviceAirportList) {
            TrainVo trainVo = new TrainVo();
            trainVo.setAirportCode(ktserviceAirport.getAirportCode());
            trainVo.setAirportName(ktserviceAirport.getAirportName());
            trainVo.setLetter(ktserviceAirport.getShortPinyin().substring(0, 1));
            if (ktserviceAirport.getPopular().equals("popular")){
                trainVo.setPopular("true");
            }
            else {
                trainVo.setPopular("false");
            }
            List<KtserviceAirport> cityIdList=ktserviceAirportMapper.selectKtserviceAirportByCityName(cityName);

            Long cityId = 0l;

            for (KtserviceAirport ka : cityIdList) {
                cityId=ka.getCityId();
            }

            if (cityId.equals(ktserviceAirport.getCityId())){
                trainVo.setCurrentCity("true");
            }
            else {
                trainVo.setCurrentCity("false");
            }
            trainVoList.add(trainVo);
        }
        return trainVoList;
    }

    /**
     * 查询热门城市
     * */
    @Override
    public List<CitiesVo> selectKtserviceAirportByPopularCities() {
        List<KtserviceAirport> ktserviceAirportList = ktserviceAirportMapper.selectKtserviceAirportByPopularCities();
        //去除重复集合元素
        List<KtserviceAirport> distinctClass = ktserviceAirportList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getCityId()))), ArrayList::new));
        List<CitiesVo> citiesVoList = new ArrayList<>();
        for (KtserviceAirport aClass : distinctClass) {
            CitiesVo citiesVo = new CitiesVo();
            citiesVo.setCityId(aClass.getCityId());
            citiesVo.setCityName(aClass.getCityName());
            citiesVoList.add(citiesVo);
        }
        return citiesVoList;
    }

    /**
     * 查询当前城市的全部机场
     * */
    @Override
    public CitiesAirporVo selectCitiesAirporVoByPopularCities(String lng, String lat, String classIficationType) {
        String cityName = cityName(lng, lat);//城市名称
        List<KtserviceAirport> cityIdList = ktserviceAirportMapper.selectKtserviceAirportByCityName(cityName);
        List<KtserviceAirportVo> ktserviceAirportVoList = new CopyOnWriteArrayList<>();
        CitiesAirporVo citiesAirporVo = new CitiesAirporVo();
        citiesAirporVo.setCitiesName(cityName);
        Long cityId = 0l;

        for (KtserviceAirport ka : cityIdList) {
            cityId=ka.getCityId();
        }

        List<KtserviceAirport> ktserviceAirportList = ktserviceAirportMapper.selectKtserviceAirportByAirport(cityId, classIficationType);

        for (KtserviceAirport ktserviceAirport : ktserviceAirportList) {
            KtserviceAirportVo ktserviceAirportVo = new KtserviceAirportVo();
            ktserviceAirportVo.setAirportCode(ktserviceAirport.getAirportCode());
            ktserviceAirportVo.setAirportName(ktserviceAirport.getAirportName());
            ktserviceAirportVo.setCityId(ktserviceAirport.getCityId());
            ktserviceAirportVo.setCityName(ktserviceAirport.getCityName());
            ktserviceAirportVo.setLng(ktserviceAirport.getLng());
            ktserviceAirportVo.setLat(ktserviceAirport.getLat());
            ktserviceAirportVo.setClassIficationType(ktserviceAirport.getClassIficationType());

            String strLngAndLat=ktserviceAirportVo.getLng()+","+ktserviceAirportVo.getLat();
            ktserviceAirportVo.setDistance(distanceMeter(lng, lat, strLngAndLat));

            ktserviceAirportVoList.add(ktserviceAirportVo);
        }

        /**
         * 按照当前坐标与机场的距离进行升序排列
         * */
        ktserviceAirportVoList.sort((x, y) -> Double.compare(x.getDistance(), y.getDistance()));

        for (int i = 0 ; i < ktserviceAirportVoList.size() ; i++) {
            if (i == 0){
                ktserviceAirportVoList.get(i).setCurrentOutlets("true");
            }
            else {
                ktserviceAirportVoList.get(i).setCurrentOutlets("false");
            }
        }

        citiesAirporVo.setKtserviceAirportVoList(ktserviceAirportVoList);
        return citiesAirporVo;
    }
}
